<template>
  <div class="ma-content-block lg:flex justify-between">
    <!-- CRUD 组件 -->
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item field="uuid" label="用户ID">
            <a-input v-model="searchForm.uuid" placeholder="请输入用户ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="user_keyword" label="用户信息搜索">
            <a-input v-model="searchForm.user_keyword" placeholder="请输入用户手机号/昵称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item field="order_no" label="审核状态">
            <sa-select v-model="searchForm.status" dict="refund_status"></sa-select>
          </a-form-item>
        </a-col>
       
      </template>

      <!-- Table 自定义渲染 -->
      <!-- 状态列 -->
       <template #id="{ record }">
        {{ record.uuid }}/{{ record.nickname }}
      </template>
      <template #operationAfterExtend="{ record }">
        <a-popconfirm
          content="确定要通过审核吗?"
          position="bottom"
          @ok="passAction(record)">
          
          <a-button v-if="record.status === 1" type="primary">
            通过
          </a-button>
        </a-popconfirm>
        <a-button v-if="record.status === 1" type="primary" @click="showModal(record)">
           驳回
        </a-button>
      </template>
      
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />

    <!-- 查看表单 -->
    <view-form ref="viewRef" @success="refresh" />
    <a-modal v-model:visible="visible" width="380px" title="驳回" ok-text="确认" cancel-text="取消" @ok="hideModal">
      <div style="margin-bottom: 10px;color: #666;">驳回原因 :</div>
      <a-input v-model="seatForm.fail_reason" placeholder="请输入驳回原因" allow-clear />
    </a-modal>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import ViewForm from './view.vue'
import api from '@/api/leader'

const crudRef = ref()
const editRef = ref()
const viewRef = ref()
const visible = ref(false)
// 搜索表单
const searchForm = ref({
  uuid: '',
  user_keyword: '',
  status: '',
})

const seatForm = ref({
  log_id: '',
  fail_reason: '',
  status: 3,
})


// SaTable 基础配置
const options = reactive({
  api: api.refundLogList,
  rowSelection: { showCheckedAll: true },
})

// SaTable 列配置
const columns = reactive([
  { title: '用户ID/昵称', dataIndex: 'id', width:180 },
  { title: '姓名', dataIndex: 'name', width: 150 },
  { title: '退款单号', dataIndex: 'out_detail_no', width: 200 },
  { title: '总充值', dataIndex: 'total_sum_price', width: 100 },
  { title: '总退款', dataIndex: 'total_withdraw_price', width: 100 },
  { title: '红包余额', dataIndex:'balance3', width:100},
  { title: '退款金额', dataIndex: 'price', width: 100 },
  { title: '审核状态', dataIndex: 'status', type: 'dict', dict: 'refund_status', width: 180 },
  { title: '申请时间', dataIndex: 'created_at', width: 180 },
  { title: '审核时间', dataIndex: 'handle_time', width: 180 },
  { title: '驳回原因', dataIndex: 'fail_reason', width: 200 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

const passAction = async (record) => {
  await api.refundAudit({ log_id: record.id, status: 2 })
  Message.success('操作成功')
  refresh()
}

const refuseAction = async () => {
  await api.refundAudit({ log_id: seatForm.value.log_id, status: 3, fail_reason: seatForm.value.fail_reason })
  Message.success('操作成功')
  refresh()
}

const showModal = (record) => {
  seatForm.value.log_id = record.id
  visible.value = true
}

const hideModal = (record) => {
  visible.value = false
  refuseAction()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
